﻿using BLL.Interfaces;
using DAL;
using GUI.Models;
using POCOS;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using WebMatrix.WebData;
using System.Net;
using Mvc.Mailer;
using GUI.Mailers;
using DotNet.Highcharts.Options;
using DotNet.Highcharts.Helpers;
using GUI.Models.Charts;
using DotNet.Highcharts;
using GUI.Models.AdminModels;
using Newtonsoft.Json;

namespace GUI.Controllers
{
    public class TestController : Controller
    {
        //
        // GET: /Test/
        private AuteurContext db = new AuteurContext();
        private IArticlesServices _articleServices;
        private IAuteurServices _auteruservice;
        public TestController(IArticlesServices articleServices)
        {
            _articleServices = articleServices;

        }

        [Authorize]
        public ActionResult Index()
        {
            var art = _articleServices.GetArticle();
            int id = WebSecurity.CurrentUserId;
            Session["idUser"] = id;

            HomeIndexViewModel viewModel = new HomeIndexViewModel();

            var author = db.Auteurs.ToList();

            Highcharts chart = new Highcharts("charts")
            .SetXAxis(new XAxis
            {
                Categories =
                new[] { "Janvier ", "Fevrier", "Mars", "Avril","Mai","Juin","Juillet","Aout","Septembre","Octobre","Novembre","Decembre"}

            })
            .SetSeries(new Series
            {
                Data =new Data(new object []{
                29,71.5,106.4,129.2
                })
            });

            viewModel.Chart = chart;
            return View  (viewModel);




        }
        public ActionResult CustomEvent()
        {
            return View();
        }

        public ActionResult Angular()
        {
            return View();
        }

        public ActionResult Ajax()
        {
            return View();
        }
        public JsonResult GetAuthors()
        {
            var authors = from a in db.Auteurs
                          select (new AuteurModel
                          {
                              Id = a.Id,
                              Name = a.Name,
                              Lastname = a.Lastname,
                              Adressmail = a.Adressmail
                          });

            return Json(new { res = authors }, JsonRequestBehavior.AllowGet);
        }
        public JsonResult GetAngulars()
        {
           
            var authors = from a in db.Auteurs
                          select (new AuteurModel
                          {
                              Id = a.Id,
                              Name = a.Name,
                              Lastname = a.Lastname,
                              Adressmail = a.Adressmail
                          });

            var jsonTodata = JsonConvert.SerializeObject(authors);
            return Json(jsonTodata, JsonRequestBehavior.AllowGet);
        }
        //public JsonResult ArticleList(int jtStartIndex = 0, int jtPageSize = 0, string jtSorting = null)
        //{
        //    try
        //    {
        //        Thread.Sleep(200);
        //        int articlcount = _articleServices.GetArticleCount();
        //        var articles = _articleServices.GetArticles(jtStartIndex, jtPageSize, jtSorting);


        //        //_articleServices._articleServices
        //        int id = Convert.ToInt32(Session["idUser"]);
        //        string name = _articleServices.GetAuthorName(id);
        //        var result = from x in articles

        //                     select new ArticleModel
        //                     {
        //                         Id = x.Id,
        //                         Title = x.Title,
        //                         CreationDate = x.CreationDate,
        //                         ArticleContenent = x.ArticleContenent

        //                     };
        //        return Json(new { Result = "OK", Records = result, TotalRecordCount = articlcount }, JsonRequestBehavior.AllowGet);
        //    }
        //    catch (Exception ex)
        //    {
        //        return Json(new { Result = "ERROR", Message = ex.Message });
        //    }
        //}

        //[HttpPost]
        //public JsonResult GetArticlesByFilter(string title = "", int auteurid = 0, int jtStartIndex = 0, int jtPageSize = 0, string jtSorting = null)
        //{
        //    try
        //    {
        //        var studentCount = _articleServices.GetArticleCountByFilter(title, auteurid);
        //        var students = _articleServices.GetArticlesByFilter(title, auteurid, jtStartIndex, jtPageSize, jtSorting);
        //        return Json(new { Result = "OK", Records = students, TotalRecordCount = studentCount });
        //    }
        //    catch (Exception ex)
        //    {
        //        return Json(new { Result = "ERROR", Message = ex.Message });
        //    }


        //}

        //[HttpPost]
        //public JsonResult CreateArticle(Article article)
        //{
        //    try
        //    {
        //        if (!ModelState.IsValid)
        //        {
        //            return Json(new { Result = "ERROR", Message = "Form is not valid! Please correct it and try again." });
        //        }


        //        _articleServices.CreateArticle(article);
        //        return Json(new { Result = "OK", Record = article });
        //    }
        //    catch (Exception ex)
        //    {
        //        return Json(new { Result = "ERROR", Message = ex.Message });
        //    }
        //}


        //[HttpPost]




        //public JsonResult UpdateArticle(ArticleModel Model)
        //{

        //    var article = _articleServices.GetArticleById(Model.Id);

        //    if (article == null)
        //    {
        //        return Json(new { Status = "Failed" }, JsonRequestBehavior.AllowGet);
        //    }
        //    else
        //    {
        //        article.Id = Model.Id;
        //        article.Title = Model.Title;
        //        article.CreationDate = Model.CreationDate;

        //        _articleServices.UpdateArticle(article);

        //      return Json(new { Result = "OK" });
        //    }
        //}






        //public JsonResult DeleteArticle(ArticleModel Model)
        //{

        //        _articleServices.DeleteArticle(Model.Id);
        //        return Json(new { Result = "OK" });


        //}


      

     
        //private IUserMailer _mailer = new UserMailer();
        //public IUserMailer Mailer
        //{
        //    get { return _mailer; }
        //    set { _mailer = value; }
        //}
        //public ActionResult IndexM()
        //{
        //    IUserMailer mailer = new UserMailer();
        //    mailer.Welcome().Send();
        //    return View();
        //}

        //public virtual MailMessage Welcome()
        //{
        //    var mailMessage = new MailMessage { Subject = "Welcome" };

        //    mailMessage.To.Add("hajan@hajan.com");
        //    ViewBag.Name = "Hajan";
        //    PopulateBody(mailMessage, viewName: "Welcome");
        //    return mailMessage;
        //}



        public JsonResult GetListAuteurs(string sidx, string sord, int page, int rows)
        {
            Auteur   arad = new Auteur();
            int pageIndex = Convert.ToInt32(page) - 1;
            int pageSize = rows;
            var listType = db.Auteurs.ToList();
            var todolist = listType.Select(x => new 
            {

                Id = x.Id,
                Name=x.Name,
                Lastname=x.Lastname,
                Profil=x.Profil,
                Adressmail=x.Adressmail,
          

            });


            int totalRecords = todolist.Count();
            var totalPages = (int)Math.Ceiling((float)totalRecords / (float)rows);
            if (sord.ToUpper() == "DESC")
            {
                todolist = todolist.OrderByDescending(s => s.Name);
                todolist = todolist.Skip(pageIndex * pageSize).Take(pageSize);
            }
            else
            {
                todolist = todolist.OrderBy(s => s.Name);
                todolist = todolist.Skip(pageIndex * pageSize).Take(pageSize);
            }
            var jsonData = new
            {
                total = totalPages,
                page,
                records = totalRecords,
                rows = todolist
            };
            return Json(jsonData, JsonRequestBehavior.AllowGet);
        }


        public JsonResult GetArticles(int Id)
        {

            var jsondata = (from a in db.Articles
                            where (a.AuteurId == Id)

                            select new
                            {
                                Id=a.Id ,
                              
                                Title=a.Title
                            });
                              
            return Json (jsondata ,JsonRequestBehavior.AllowGet );
        }


        public ActionResult Tyni()

        {
            ArticleDetail artd = new ArticleDetail();

            var Jsondata = artd;
            return View(artd);
        }
    }
}

